<?php

/*
 * 目前的config的情况如下
  $configs = array(
  'user' => array('theme_select', 'timezone'),
  'taxonomy' => array(
  'vocabulary' => array('identification' => array('description', 'help'), 'settings' => array('tags', 'multiple', 'required', 'weight')),
  'term' => array('advanced' => array('relations', 'synonyms', 'weight', 'parent')),
  ),
  );
 * 
 */

function system_reform_config_overview() {
  $output = drupal_get_form('system_reform_config_form');
  return $output;
}

function system_reform_config_form() {
  $configs = system_reform_get_config();
  $user_config = $configs['user'];
  $taxonomy_config = $configs['taxonomy'];
  $form['user'] = array(
      '#type' => 'fieldset',
      '#title' => t('Config User'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
  );
  $form['user']['user-theme_select'] = array(
      '#type' => 'checkbox',
      '#title' => t('Theme configuration'),
      '#description' => t('Selecting a different theme will change the look and feel of the site.'),
      '#default_value' => system_reform_config_in_array('theme_select', $user_config),
  );
  $form['user']['user-timezone'] = array(
      '#type' => 'checkbox',
      '#title' => t('Locale settings'),
      '#description' => t('Select your current local time. Dates and times throughout this site will be displayed using this time zone.'),
      '#default_value' => system_reform_config_in_array('timezone', $user_config),
  );

  $form['taxonomy'] = array(
      '#type' => 'fieldset',
      '#title' => t('Config Taxonomy'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
  );
  $form['taxonomy']['vocabulary'] = array(
      '#type' => 'fieldset',
      '#title' => t('Vocabulary'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
  );
  $form['taxonomy']['vocabulary']['identification'] = array(
      '#type' => 'fieldset',
      '#title' => t('Identification'),
  );
  $form['taxonomy']['vocabulary']['identification']['taxonomy-vocabulary-identification-description'] = array(
      '#type' => 'checkbox',
      '#title' => t('Description'),
      '#description' => t('Description of the vocabulary; can be used by modules.'),
      '#default_value' => system_reform_config_in_array('description', $taxonomy_config['vocabulary']['identification']),
  );
  $form['taxonomy']['vocabulary']['identification']['taxonomy-vocabulary-identification-help'] = array(
      '#type' => 'checkbox',
      '#title' => t('Help text'),
      '#description' => t('Instructions to present to the user when selecting terms, e.g., "Enter a comma separated list of words".'),
      '#default_value' => system_reform_config_in_array('help', $taxonomy_config['vocabulary']['identification']),
  );
  $form['taxonomy']['vocabulary']['settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Settings'),
  );
  $form['taxonomy']['vocabulary']['settings']['taxonomy-vocabulary-settings-tags'] = array(
      '#type' => 'checkbox',
      '#title' => t('Tags'),
      '#description' => t('Terms are created by users when submitting posts by typing a comma separated list.'),
      '#default_value' => system_reform_config_in_array('tags', $taxonomy_config['vocabulary']['settings']),
  );
  $form['taxonomy']['vocabulary']['settings']['taxonomy-vocabulary-settings-multiple'] = array(
      '#type' => 'checkbox',
      '#title' => t('Multiple select'),
      '#description' => t('Allows posts to have more than one term from this vocabulary (always true for tags).'),
      '#default_value' => system_reform_config_in_array('multiple', $taxonomy_config['vocabulary']['settings']),
  );
  $form['taxonomy']['vocabulary']['settings']['taxonomy-vocabulary-settings-required'] = array(
      '#type' => 'checkbox',
      '#title' => t('Required'),
      '#description' => t('At least one term in this vocabulary must be selected when submitting a post.'),
      '#default_value' => system_reform_config_in_array('required', $taxonomy_config['vocabulary']['settings']),
  );
  $form['taxonomy']['vocabulary']['settings']['taxonomy-vocabulary-settings-weight'] = array(
      '#type' => 'checkbox',
      '#title' => t('Weight'),
      '#description' => t('Vocabularies are displayed in ascending order by weight.'),
      '#default_value' => system_reform_config_in_array('weight', $taxonomy_config['vocabulary']['settings']),
  );
  $form['taxonomy']['term'] = array(
      '#type' => 'fieldset',
      '#title' => t('Term'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
  );
  $form['taxonomy']['term']['advanced'] = array(
      '#type' => 'fieldset',
      '#title' => t('Advanced options'),
  );
  $form['taxonomy']['term']['advanced']['taxonomy-term-advanced-parent'] = array(
      '#type' => 'checkbox',
      '#title' => t('Parents'),
      '#description' => t('Parent terms'),
      '#default_value' => system_reform_config_in_array('parent', $taxonomy_config['term']['advanced']),
  );
  $form['taxonomy']['term']['advanced']['taxonomy-term-advanced-relations'] = array(
      '#type' => 'checkbox',
      '#title' => t('Related terms'),
      '#description' => t('Related terms'),
      '#default_value' => system_reform_config_in_array('relations', $taxonomy_config['term']['advanced']),
  );
  $form['taxonomy']['term']['advanced']['taxonomy-term-advanced-synonyms'] = array(
      '#type' => 'checkbox',
      '#title' => t('Synonyms'),
      '#description' => t('Synonyms of this term, one synonym per line.'),
      '#default_value' => system_reform_config_in_array('synonyms', $taxonomy_config['term']['advanced']),
  );
  $form['taxonomy']['term']['advanced']['taxonomy-term-advanced-weight'] = array(
      '#type' => 'checkbox',
      '#title' => t('Weight'),
      '#description' => t('Terms are displayed in ascending order by weight.'),
      '#default_value' => system_reform_config_in_array('weight', $taxonomy_config['term']['advanced']),
  );
  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save page'),
  );

  return $form;
}

function system_reform_config_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $configs = array();
  foreach ($values as $key => $value) {
    if (count($ks = explode('-', $key)) > 1 && $value == 1) {
      $string = '';
      $name = array_pop($ks);
      foreach ($ks as $k => $v) {
        $string .= '["' . $v . '"]';
      }
      $string = '$configs' . $string . '[] = ' . $name;
      eval('return ' . $string . ';');
    }
  }
  $modules = array('user', 'taxonomy');
  foreach ($modules as $k => $m) {
    $config = empty($configs[$m]) ? array() : $configs[$m];
    $data = array('module' => $m, 'config' => $config);
    db_query('delete from {system_reform_config} where module = "%s"', $m);
    drupal_write_record('system_reform_config', $data);
  }
}

function system_reform_config_in_array($value, $array) {
  if (empty($array)) {
    return 0;
  }
  return in_array($value, $array) ? 1 : 0;
}

?>